package pack

import (
	"mall_admin_api/biz/model"
	"mall_admin_api/biz/model/admin_product_category"
)

func GetCategoryList(list []*model.Category) []*admin_product_category.Category {
	var data = make([]*admin_product_category.Category, 0)
	if len(list) == 0 {
		return data
	}
	return categoryTree(list, 0)
}

func Category(category *model.Category) *admin_product_category.Category {
	return &admin_product_category.Category{
		ID:   category.ID,
		Name: category.Name,
		PID:  category.Pid,
	}
}

func categoryTree(list []*model.Category, pid int64) []*admin_product_category.Category {
	var data = make([]*admin_product_category.Category, 0)
	for _, category := range list {
		if category.Pid == pid {
			var temp = admin_product_category.NewCategory()
			temp.ID = category.ID
			temp.Name = category.Name
			temp.PID = category.Pid
			temp.Sort = category.Sort
			temp.Label = category.Name
			temp.Children = categoryTree(list, category.ID)
			data = append(data, temp)
		}
	}
	return data
}

func GetSelectCategoryList(list []*model.Category) []*admin_product_category.SelectCategory {
	var data = make([]*admin_product_category.SelectCategory, 0)
	if len(list) == 0 {
		return data
	}
	return selectCategoryTree(list, 0, "|----")
}

func selectCategoryTree(list []*model.Category, pid int64, pre string) []*admin_product_category.SelectCategory {
	var data = make([]*admin_product_category.SelectCategory, 0)
	for _, category := range list {
		if category.Pid == pid {
			var temp = admin_product_category.NewSelectCategory()
			temp.ID = category.ID
			temp.Label = pre + category.Name
			data = append(data, temp)
			childrenList := selectCategoryTree(list, category.ID, pre+"|----")
			temp.Disabled = 1
			if len(childrenList) > 0 {
				data = append(data, childrenList...)
				temp.Disabled = 0
			}
		}
	}
	return data
}
